package com.one.group.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.CollectMaterialsInsertDto;
import com.one.group.model.dto.CollectMaterialsQueryDto;
import com.one.group.model.vo.CollectMaterialsListVo;
import com.one.group.result.Result;
import com.one.group.service.CollectMaterialsService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("collectMaterials")
@Tag(name = "维修领退费管理接口")
public class CollectMaterialsController {
    @Autowired
    CollectMaterialsService collectMaterialsService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @PostMapping("list")
    @Operation(summary = "查询维修领退费列表")
    public Result list(@RequestBody CollectMaterialsQueryDto collectMaterialsQueryDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        collectMaterialsQueryDto.setTenantId(tenantId);
        PageHelper.startPage(collectMaterialsQueryDto.getPage(),collectMaterialsQueryDto.getLimit());
        List<CollectMaterialsListVo> collectMaterialsListVos = collectMaterialsService.findCollectMaterialsByQueryDto(collectMaterialsQueryDto);
        PageInfo<CollectMaterialsListVo> pageInfo = new PageInfo<>(collectMaterialsListVos);
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询维修领退费信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(collectMaterialsService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有维修领退费信息")
    public Result findAll() {
        return ResultUtil.success(collectMaterialsService.list());
    }

    @PostMapping("/add")
    @Operation(summary = "添加维修领退费信息")
    public Result add(@RequestBody CollectMaterialsInsertDto collectMaterialsAddDto){

        if (collectMaterialsService.insertCollectMaterials(collectMaterialsAddDto)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
}
